using GameFramework.Update;

namespace GameFramework.Gameplay
{
    /// <summary>
    /// 玩法抽象类
    /// </summary>
    public abstract class AGameplay : IGameplay, IFixedUpdate, IUpdate
    {
        public abstract bool IsFinish { get; }

        private bool m_Initialized;

        private readonly IGameplayModelContainer m_ModelContainer = new GameplayModelContainer();
        private readonly IGameplaySystemContainer m_SystemContainer = new GameplaySystemContainer();
        private readonly IGameplayViewControllerContainer m_ViewControllerContainer = new GameplayViewControllerContainer();

        public void Initialize()
        {
            if (m_Initialized)
                return;

            m_Initialized = true;

            OnAddModels(m_ModelContainer);
            OnAddSystems(m_SystemContainer);
            OnAddViewControllers(m_ViewControllerContainer);

            m_ModelContainer.Initialize();
            m_SystemContainer.Initialize();
            m_ViewControllerContainer.Initialize();

            UpdateService.That.RegisterFixedUpdate(this);
            UpdateService.That.RegisterUpdate(this);
        }

        public void Dispose()
        {
            if (!m_Initialized)
                return;

            m_Initialized = false;

            UpdateService.That.UnRegisterUpdate(this);
            UpdateService.That.UnRegisterFixedUpdate(this);

            m_ModelContainer.Dispose();
            m_SystemContainer.Dispose();
            m_ViewControllerContainer.Dispose();
        }

        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            m_ViewControllerContainer.OnUpdate(elapseSeconds, realElapseSeconds);
        }

        public void OnFixedUpdate(float elapseSeconds, float realElapseSeconds)
        {
            m_SystemContainer.OnFixedUpdate(elapseSeconds, realElapseSeconds);
        }

        protected abstract void OnAddModels(IGameplayModelContainer container);
        protected abstract void OnAddSystems(IGameplaySystemContainer container);
        protected abstract void OnAddViewControllers(IGameplayViewControllerContainer container);
    }
}